package com.manager.cms.config.interceptor;

import com.manager.cms.annotation.IgnoreLogin;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            // 配置该注解，说明不进行登录拦截
            IgnoreLogin annotation = handlerMethod.getBeanType().getAnnotation(IgnoreLogin.class);
            if (annotation == null) {
                annotation = handlerMethod.getMethodAnnotation(IgnoreLogin.class);
            }
            if (annotation != null) {
                return true;
            }
        }

        // 登录验证
       /* String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            RequestDispatcher view = request.getRequestDispatcher("/error/tokenError");
            view.forward(request, response);
            return false;
        }*/

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        super.afterCompletion(request, response, handler, ex);
    }
}
